package dream.web;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import dream.pojo.Article;
import dream.service.ArticleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/artdetail")
public class ArtDetailServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Article article=new Article();
        //1.得到前端的数据并校验
        String aidStr=req.getParameter("aid");

        if(!StrUtil.isBlank(aidStr)) {
            //转为int类型并查询
            article = articleService.SelectByAid(Integer.valueOf(aidStr));


            // 转为JSON
            String jsonString = JSON.toJSONString(article);

            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }else {
            resp.getWriter().write("0");
        }
    }
}
